package com.shop.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.shop.Utils.ResponseMessage;
import com.shop.exception.zdyException;
import com.shop.po.User;
import com.shop.service.UserService;


@Controller
public class registController {
	@Autowired
	private UserService userService;

	//跳转到注册界面
	@RequestMapping("regist")
	public String regist() {
		return "regist";
	}
	
	//用户注册
	@RequestMapping(value = "/userRegist", produces = { "application/json;charset=UTF-8" }, method = RequestMethod.POST)
	@ResponseBody
	public ResponseMessage userRegist(HttpServletRequest request,
			@Validated User user,@RequestParam String checkImg,@RequestParam String rePassword) throws Exception {
		request.setAttribute("registUser",user);
		request.setAttribute("rePassword",rePassword);
		if(user.getUsername() == null || user.getUsername() == "") {
			return ResponseMessage.error().addObject("registMe", "输入用户名不能为空!");
		}
		if(user.getPassword() == null || user.getPassword() == "") {
			return ResponseMessage.error().addObject("registMe", "输入密码不能为空!");
		}
		if(rePassword == null || rePassword == "" ) {
			return ResponseMessage.error().addObject("registMe", "确认密码不能为空!");
		}
		if(user.getEmail() == null || user.getEmail() == "") {
			return ResponseMessage.error().addObject("registMe", "输入邮箱不能为空!");
		}
		if(user.getName() == null || user.getName() == "") {
			return ResponseMessage.error().addObject("registMe", "输入姓名不能为空!");
		}
		if(user.getPhone() == null || user.getPhone() == "") {
			return ResponseMessage.error().addObject("registMe", "输入电话不能为空!");
		}
		if(user.getAddr() == null || user.getAddr() == "") {
			return ResponseMessage.error().addObject("registMe", "输入地址不能为空!");
		}
		if(user.getQuestion() == null || user.getQuestion() == "") {
			return ResponseMessage.error().addObject("registMe", "输入密保问题不能为空!");
		}
		if(user.getAnswer() == null || user.getAnswer() == "") {
			return ResponseMessage.error().addObject("registMe", "输入密保答案不能为空!");
		}
		String sessionCode = (String) request.getSession().getAttribute("checkcode");
		if (!sessionCode.equalsIgnoreCase(checkImg)) {
			return ResponseMessage.error().addObject("registMe", "验证码错误,请重新输入!");
		}else {
			userService.saveUser(user);
			return ResponseMessage.success().addObject("registMe", "注册成功请登录");
		}
		
	}


	//判断用户名是否存在
	@RequestMapping(value = "/registFindByid", produces = { "application/json;charset=UTF-8" }, method = RequestMethod.POST)
	@ResponseBody
	public ResponseMessage registFindByid(HttpServletRequest request,
			 @RequestParam String username) throws Exception {
		User user = userService.loginFindByid(username);
		if (user!=null) {
			return ResponseMessage.success().addObject("zhuce", "用户名已存在");
		}else{
			return ResponseMessage.error().addObject("zhuce", "可以注册");
		}
	}
}
